#include "test2.h"

// -----------------------------------------------------------------------------
void
tenacitas::balance_line::test2::
run( ) {

    f0( );
    f1( );
    
}

const char * say_what(bool b) { return b ? "true" : "false"; }        

// if (
//     ( l_size > 5 ) &&
//     ( l_filename[l_size - 1] == 'D' ) &&
//     ( l_filename[l_size - 2] == 'N' ) &&
//     ( l_filename[l_size - 3] == 'E' ) &&
//     ( l_filename[l_size - 4] == '.' ) ) {
//     std::cout << l_filename << ", ";

//     std::string::size_type l_end = l_size - 8;

//     std::string::size_type l_pos = l_end;
//     for ( ; l_filename[l_pos] != '_'; --l_pos ) {
//         ;
//     }
//     ++l_pos;
                        

//     std::string l_index_str ( &l_filename[l_pos], &l_filename[l_end] );
//     std::cout << l_index_str << ", ";

//     std::uint64_t l_index = std::stoull( l_index_str );
//     std::cout << l_index << "\n";
            
// }        


// -----------------------------------------------------------------------------
void
tenacitas::balance_line::test2::
f1( ) {
//    logger::ptr l_logger ( logger::create( ) );

    // merging log

#ifndef DIR_LOG
    DIR_LOG = "./"
#endif

        filesystem::directory_iterator l_dir_begin( DIR_LOG );
    
    filesystem::directory_iterator l_dir_end;

    merger_all<log_file>::ptr l_merger = merger_all<log_file>::create( );
    

    for( boost::filesystem::directory_iterator l_dir_ite = l_dir_begin;
         l_dir_ite != l_dir_end;
         ++l_dir_ite ) {

        std::string l_filename = l_dir_ite->path( ).filename( ).string( );

        std::cout << "possible log file: '" << l_filename << "'... " ;

        std::string::size_type l_end_pos = l_filename.find( ".END" );        
        std::string l_filename_wo_end;
        
        
        if ( l_end_pos != std::string::npos ) {

            // we accept only if the original file, i.e., without the .END,
            // really does not exist anymore 
            l_filename_wo_end = std::string( DIR_LOG ) +
                std::string( "/") +
                l_filename.substr( 0, l_end_pos ); 

            if ( ( boost::filesystem::exists( l_filename_wo_end ) ) &&
                 ( boost::filesystem::is_regular_file( l_filename_wo_end ) ) ) {

                std::cout << "rejected, because " << l_filename_wo_end
                          << " exists!" << std::endl;
            }
            
            else {

                // it is a log file
                std::cout << "accepted" <<  std::endl;
                l_merger->add( log_file::ptr( new log_file( std::string( DIR_LOG ) +
                                                            std::string( "/") +
                                                            l_filename ) ) );
            }
        }
        else {
            std::cout << "rejected, because it does not end in '.END'" <<
                std::endl; 
        }
    }

    output l_output;    
    l_merger->start( l_output );

}

// -----------------------------------------------------------------------------
void
tenacitas::balance_line::test2::
f2( std::int16_t p_repeats,
    std::int16_t p_stop_at,
    std::int32_t p_sleep,
    const std::string p_prefix ) {

    logger::ptr l_logger ( logger::create( ) );
    
    std::int16_t l_counter = 0;

    log_trace( l_logger ) << p_prefix << ", "
                          << p_repeats << ", "
                          << p_stop_at << ", "
                          << p_sleep;

    
    for ( std::int16_t l_i = 0; l_i < p_repeats; ++l_i ) {
        if ( l_counter >= p_stop_at ) {
            log_trace( l_logger ) << p_prefix << " counter = " << l_counter
                                  << ", so gone sleep";
            
            boost::this_thread::sleep(boost::posix_time::milliseconds(p_sleep));
            l_counter = 0;
            log_trace( l_logger ) << p_prefix << " woke up";
        }

        ++l_counter;

        log_trace( l_logger ) << p_prefix << ", " << l_i << " executions";        
    }
    std::cout << p_prefix << " leaving"  << std::endl;
}

// -----------------------------------------------------------------------------
void
tenacitas::balance_line::test2::
f0( ) {

    boost::thread l_01( f2, 100, 20, 200, "one");

    boost::thread l_02( f2, 300, 250, 500, "two");

    boost::thread l_03( f2, 1000, 800, 300, "three");

    boost::thread l_04( f2, 200, 30, 900, "four");

    boost::thread l_05( f2, 5000, 1000, 1000, "five"); 

    boost::thread l_06( f2, 100, 1, 200, "six");

    boost::thread l_07( f2, 700, 130, 300, "seven");

    boost::thread l_08( f2, 2000, 1000, 100, "eight");

    boost::thread l_09( f2, 900, 50, 1000, "nine");

    boost::thread l_10( f2, 10, 2, 500, "ten");

    boost::thread l_11( f2, 5, 2, 200, "eleven");

    l_01.join( );
    l_02.join( );
    l_03.join( );
    l_04.join( );
    l_05.join( );
    l_06.join( );
    l_07.join( );
    l_08.join( );
    l_09.join( );
    l_10.join( );
    l_11.join( );

//    boost::this_thread::sleep(boost::posix_time::seconds(2));
}
